//
//  TYEmptyView.m
//  TYUIKit
//
//  Created by TuyaInc on 2019/5/22.
//

#import "TYEmptyView.h"

#import "TYUIKit.h"

@interface TYEmptyView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;

@property (nonatomic, strong) UIView *actionContainer;

@end

@implementation TYEmptyView

- (instancetype)init {
    if (self = [super init]) {
        [self initial];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initial];
    }
    return self;
}

- (void)initial {
    _titleColor = TY_RGBColor(163, 163, 170);
    _titleFont = [UIFont systemFontOfSize:13];
    
    _titleOffset = CGPointMake(0, 14);
    _subtitleOffset = CGPointMake(0, 14);
    _actionButtonOffset = CGPointMake(0, 14);
    
    _centerLocation = CGPointMake(0.5, 4/9.0);
}

#pragma mark - TYEmptyViewProtocol

- (void)ty_emptyViewShouldLayout {
    self.ty_width = self.superview.ty_width;
    
    CGFloat h = 0;
    if (_image) {
        if (!_imageView) {
            _imageView = [UIImageView new];
        }
        if (_imageView.superview != self) {
            [self addSubview:_imageView];
        }
        _imageView.image = _image;
        _imageView.ty_size = CGSizeEqualToSize(_imageViewSize, CGSizeZero) ? _image.size : _imageViewSize;
        _imageView.ty_centerX = self.ty_width/2.0 + _imageOffset.x;
        _imageView.ty_top = _imageOffset.y;
        h = _imageView.ty_bottom;
    } else {
        [_imageView removeFromSuperview];
    }

    if (_title.length > 0) {
        if (!_titleLabel) {
            _titleLabel = [UILabel new];
            _titleLabel.font = _titleFont;
            _titleLabel.textColor = _titleColor;
            _titleLabel.numberOfLines = 0;
        }
        if (_titleLabel.superview != self) {
            [self addSubview:_titleLabel];
        }
        _titleLabel.text = _title;
        CGSize size = [_titleLabel sizeThatFits:CGSizeMake(self.ty_width - 20, CGFLOAT_MAX)];
        _titleLabel.ty_size = size;
        _titleLabel.ty_centerX = self.ty_width / 2.0 + _titleOffset.x;
        _titleLabel.ty_top = h + _titleOffset.y;
        h = _titleLabel.ty_bottom;
    } else {
        [_titleLabel removeFromSuperview];
    }
    
    if (_subtitle.length > 0) {
        if (!_subtitleLabel) {
            _subtitleLabel = [UILabel new];
            _subtitleLabel.font = _subtitleFont;
            _subtitleLabel.textColor = _subtitleColor;
            _subtitleLabel.numberOfLines = 0;
        }
        if (_subtitleLabel.superview != self) {
            [self addSubview:_subtitleLabel];
        }
        _subtitleLabel.text = _subtitle;
        CGSize size = [_subtitleLabel sizeThatFits:CGSizeMake(self.ty_width - 20, CGFLOAT_MAX)];
        _subtitleLabel.ty_size = size;
        _subtitleLabel.ty_centerX = self.ty_width/2.0 - _subtitleOffset.x;
        _subtitleLabel.ty_top = h + _subtitleOffset.y;
        h = _subtitleLabel.ty_bottom;
    } else {
        [_subtitleLabel removeFromSuperview];
    }
    
    if (_actionArr) {
        if (!_actionContainer) {
            _actionContainer = [UIView new];
        }
        if (_actionContainer.superview != self) {
            [_actionContainer ty_removeAllSubviews];
            [self addSubview:_actionContainer];
        }
        _actionContainer.ty_height = 8;
        
        CGFloat maxBtnH = 0;
        
        for (NSInteger idx = 0; idx < _actionArr.count; idx ++) {
            UIButton *btn = _actionArr[idx];
            btn.ty_centerY = _actionContainer.ty_height/2.0;
            btn.ty_left = idx == 0 ? 0 : (_actionArr[idx - 1].ty_right + _actionButtonSpace);
            maxBtnH = MAX(maxBtnH, btn.ty_height);
            [_actionContainer addSubview:btn];
        }
        _actionContainer.ty_width = _actionArr.lastObject.ty_right;
        _actionContainer.ty_height = maxBtnH;
        _actionContainer.ty_top = h + _actionButtonOffset.y;
        _actionContainer.ty_centerX = self.ty_width/2.0;
        h = _actionContainer.ty_bottom;
        for (NSInteger idx = 0; idx < _actionArr.count; idx ++) {
            UIButton *btn = _actionArr[idx];
            btn.ty_centerY = _actionContainer.ty_height/2.0;
        }
    } else {
        [_actionContainer removeFromSuperview];
        _actionContainer = nil;
    }
    
    self.ty_height = h;
    
    self.ty_centerX = self.superview.ty_width * _centerLocation.x;
    self.ty_centerY = self.superview.ty_height * _centerLocation.y;
}

@end
